package com.fuli.app.controller;

import com.fuli.app.common.ActionResult;
import com.fuli.app.common.ErrorInfo;
import com.fuli.app.common.Pagination;
import com.fuli.app.dto.ReviewDTO;
import com.fuli.app.dto.query.ReviewQueryDTO;
import com.fuli.app.service.ReviewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "评论")
@RequestMapping("/review")
public class ReviewController {
    @Autowired
    private ReviewService reviewService;

    @ApiOperation(value = "添加评论",
            notes = " <b>必传字段：</b><br/>" +
                    "1.content 评论内容<br/> " +
                    "2.userId 评论人id<br/> " +
                    "3.workId 作业id")
    @PostMapping("/add")
    public ActionResult<String> add(@ApiParam("评论DTO") @RequestBody ReviewDTO dto) {
        List<ErrorInfo> errors = new ArrayList<>();
        if (StringUtils.isEmpty(dto.getContent()))
            errors.add(new ErrorInfo(0, "评论内容为空"));
        if (StringUtils.isEmpty(dto.getWorkId()))
            errors.add(new ErrorInfo(0, "作业id为空"));
        if (StringUtils.isEmpty(dto.getUserId()))
            errors.add(new ErrorInfo(0, "评论人id为空"));
        if (!errors.isEmpty())
            return new ActionResult<>(errors);
        return reviewService.add(dto);
    }

    @ApiOperation("删除评论")
    @DeleteMapping(value = "/delete/{id}")
    public ActionResult<String> delete(@ApiParam("id") @PathVariable String id) {
        return reviewService.delete(id);
    }

    @ApiOperation("批量删除评论")
    @DeleteMapping(value = "/batchDelete")
    public ActionResult<List<String>> batchDelete(@ApiParam("评论id数组") @RequestParam List<String> ids) {
        return reviewService.batchDelete(ids);
    }

    @ApiOperation("获取评论")
    @GetMapping(value = "/get/{id}")
    public ActionResult<ReviewDTO> get(@ApiParam("id") @PathVariable String id) {
        return reviewService.get(id);
    }

    @ApiOperation("通过作业id获取评论")
    @GetMapping(value = "/getByWorkId/{workId}")
    public ActionResult<List<ReviewDTO>> getByWorkId(@ApiParam("workId") @PathVariable String workId) {
        return reviewService.getByWorkId(workId);
    }

    @ApiOperation("分页获取评论")
    @GetMapping(value = "/pageQuery")
    public ActionResult<Pagination<ReviewDTO>> listByQueryDTO(@ApiParam("评论查询DTO") @ModelAttribute ReviewQueryDTO QueryDTO) {
        return reviewService.listByQueryDTO(QueryDTO);
    }

    @ApiOperation("通过作业id获取评论")
    @GetMapping(value = "/getByUserId/{userId}")
    public ActionResult<List<ReviewDTO>> getByUserId(@ApiParam("userId") @PathVariable String userId) {
        return reviewService.getByUserId(userId);
    }

}
